<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/*
 * 麦口银联快捷2
 * */
class Maikou3Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;


    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));

    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //银联快捷
            case 13:
                $pay_bankcode = 'YL';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        // 构造要请求的参数数组
        $parameter = array(
            "payAppKey" => $this->config['mch_id'],
            "userID" => strval(mt_rand(1,10000)),
            "payChannel" => $pay_bankcode,
            "productName" =>strval(mt_rand(1,10000)),
            "orderNo" => $return['orderid'],
            "orderPrice" =>  $return['amount'],
            "orderIp" => getIP(),
            "orderPeriod" => 5,//订单有效期，单位：分钟
            "notifyUrl" => $return['notifyurl'],
            "returnUrl" => $return['callbackurl'],
        );


        ksort($parameter);
        $md5str = "";
        foreach ($parameter as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }
        $parameter['sign']= strtoupper(md5($md5str . "paySecret=" . $this->config['signkey']));

        po($this->config['gateway'], $parameter);
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();

        if(!$this->checkip(getIP(),['47.112.96.93'])){
            exit("wrong");
        }

        $p = json_decode(file_get_contents('php://input'),true);

        /*
{"payAppKey":"CYYG2019051111220020","custID":"2006","productName":"7319","orderNo":"2019051315332645722","orderPrice":"30","orderDate":"20190513","orderTime":"20190513153327","trxNo":"20190513153327","payWayCode":"YL","tradeStatus":"SUCCESS","remark":"","sign":"4672806AB0C94F875973EA681921523D"}
         * */

        //获取订单信息
        $orderid = $p['orderNo'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['trxNo'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);
        unset($p['remark']);

        ksort($p);
        $md5str = "";
        foreach ($p as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }
        $sign1 = strtoupper(md5($md5str . "paySecret=" . $this->config['signkey']));


        if($sign1 !== $sign ){
            exit("sign_wrong");
        }

        if($p['tradeStatus'] !== 'SUCCESS'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}